class Weibo < ActiveRecord::Base
  CHECK = {'未送审' => 0,'已送审' => 1,'已采用' => 2,'未采用' => 3}

  paginates_per 20

  validates_presence_of :content,:long_content

  default_scope ->{ self.order("created_at desc") }
  scope :check,->(check){check.blank? ? nil : where(:check => check.to_i)}
  scope :submited_weibos,->{where(:check => [1,2,3])}

  def check_text
    #CHECK.map{|k,v| [v,k]}.to_h[check]
    case check
      when 0
        '未送审'
      when 1
        '已送审'
      when 2
        '已采用'
      when 3
        '未采用'
    end
  end

  #通知类型（我的通知，平台通知）
  def self.weibo_type type,user
    case type
      when "1"
        self.where(:postby => user.name)
      when "2"
        if user.admin? || user.xinmeiti?
          self.submited_weibos
        else
          Weibo.where("1=2")
        end
    end
  end

  def self.can_edit? weibo,user
    if weibo.check == 0 || user.admin? || user.xinmeiti?
      true
    else
      false
    end
  end

  def self.can_approve? weibo,user
    if weibo.check == 1 && (user.admin? || user.xinmeiti?)
      true
    else
      false
    end
  end
end
